package com.douguo.recipe.sina;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

public class Device {

	private static Device instance;
	private TelephonyManager telephonyManager;
	private DisplayMetrics dm;

	private Device() {

	}

	public static Device getInstance() {
		if (instance == null) {
			instance = new Device();
		}
		return instance;
	}

	public DisplayMetrics getDisplayMetrics(Context context) {
		if (dm == null) {
			dm = context.getApplicationContext().getResources()
					.getDisplayMetrics();
		}
		return dm;
	}

	public TelephonyManager getTelephonyManager(Context context) {
		if (telephonyManager == null) {
			telephonyManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
		}
		return telephonyManager;
	}

	public String getNetType(Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);

			NetworkInfo netInfo = cm.getActiveNetworkInfo();

			if (netInfo == null) {
				return null;
			}
			String mobileTypeName = null;
			String typeName = netInfo.getTypeName();
			
			// TODO: FOR TEST!!!
//			if (true)return"cmwap";

			// 如果得到WIFI，直接返回
			if ("wifi".equalsIgnoreCase(typeName) && netInfo.isAvailable()) {
				mobileTypeName = typeName;
			} else if ("mobile".equalsIgnoreCase(typeName)
					&& netInfo.isAvailable()) {
				String extroInfo = netInfo.getExtraInfo();
				mobileTypeName = extroInfo;
			}

			return mobileTypeName;
		} catch (Exception e) {
			return null;
		}
	}
}
